﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;

public partial class Admin_students : BasePage
{
	protected void Page_Load(object sender, EventArgs e)
	{
		//if(! ((Teacher.IS_ADMIN == 1) || (Teacher.IS_CALL_CENTER == 1)))
		//{
		//    Response.Redirect("error.aspx");
		//}

		if (!IsPostBack)
		{
			Fill();
		}

	
	}

	public override string Icon
	{
		get
		{
			return "images/admin_icon.png";
		}
	}

	protected void lbExport_Click(object sender, ImageClickEventArgs e)
	{
		DataLayer dal = new DataLayer();
		string message = string.Empty;

		DataTable toExcel;
        var initial = string.IsNullOrEmpty(tbName.Text) ? Letter.ToString() : tbName.Text;

        toExcel = dal.FindStudentsAdmin(PAConfiguration.ConnectionString, initial, true, out message);

        //if (tbName.Text == string.Empty)
        //    toExcel = dal.FindStudents(PAConfiguration.ConnectionString, Letter, out message);
        //else
        //    toExcel = dal.FindStudents(PAConfiguration.ConnectionString, tbName.Text, out message);


		//PALogManager.MessageLog(message);

		DataColumn student_id = null;
		foreach (DataColumn dc in toExcel.Columns)
		{
			foreach (GridColumn dgf in rgStudents.Columns)
			{
				if (dc.ColumnName == dgf.SortExpression)
				{
					dc.ColumnName = dgf.HeaderText;
					break;
				}
				
			}

			if (dc.ColumnName == "STUDENT_ID")
			{
				student_id = dc;
			}
		}

		if (student_id != null)
		{
			toExcel.Columns.Remove(student_id);
		}
		Manager.ExportToCSV(
			toExcel,
			this.Context,
			"Students.csv");
	}


	protected char Letter
	{
		get
		{
			if (String.IsNullOrEmpty(Request.QueryString["letter"]))
			{
				return 'A';
			}
			if(string.Compare(Request.QueryString["letter"] , "all", true) == 0)
				return ' ';

			return (char)Request.QueryString["letter"][0];
		}
	}

	protected override void Fill()
	{
		DataLayer dal = new DataLayer();
		string message;

		DataTable dt = null;

        var initial = string.IsNullOrEmpty(tbName.Text) ? Letter.ToString() : tbName.Text;

        dt = dal.FindStudentsAdmin(PAConfiguration.ConnectionString, initial, true, out message);


		if (dt == null)
		{
			rgStudents.DataSource = null;
			rgStudents.DataBind();
			return;
		}


		if (!string.IsNullOrEmpty(Sorting))
		{
			dt.DefaultView.Sort = Sorting;
		}

		rgStudents.DataSource = dt;
		rgStudents.DataBind();

	}
	protected void gvStudents_Sorting(object sender, GridViewSortEventArgs e)
	{
		Sort(e);
	}
	
	protected void gvStudents_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.Header)
		{
			this.ApplySortingToHeader(e.Row.Cells);
		}
	}
	protected void bGo_Click(object sender, ImageClickEventArgs e)
	{
		Fill();
	}


	protected void rgStudents_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
	{
		Sort(e);
	}

	protected void rgStudents_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
	{
		Fill();
	}
}
